package com.javabase.y2022.m04.day0423.e02;

import java.util.Random;

/**
 * @descriptions: 大于某个数值的最右位置
 * @author: Tom
 * @date: 2022/4/23 下午 04:27
 * @version: 1.0
 */
public class Solution {
    public static void main(String[] args) {
        int[] arr = new int[]{1,3,5,7,9,11,13,15};
        Random random = new Random();
        random.nextInt(5);
        System.out.println(leftestestPosMoreAndEqual(arr, 6));
        System.out.println(rightestPosLessAndEqual(arr, 6));
    }
    public static int leftestestPosMoreAndEqual(int[] arr, int target){
        int L = 0;
        int R = arr.length - 1;
        int index = R;
        while(L <= R){
            int mid = L + ((R-L)>>1);
            if(arr[mid] > target){
                index = mid;
                R = mid - 1;
            } else {
                L = mid + 1;
            }
        }
        return  index;
    }

    public static int rightestPosLessAndEqual(int[] arr, int target){

        int L = 0;
        int R = arr.length - 1;
        int index = 0;
        while (L < R){
            int mid = L + ((R-L)>>1);
            if(arr[mid] < target){
                index = mid;
                L = mid + 1;
            } else {
                R = mid - 1;
            }
        }
        return arr[L] < target ? L : index;
    }
}
